package KK.Time
{
	import KK.System.Pool.IPool;
	import KK.System.Pool.Pool;
	import KK.System.Pool.PoolManager;
	
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	public class Time extends PoolManager
	{
		
		static private var timer:Timer;
		private var list:Vector.<S_Timer>;
		
		static private var Instance:Time;
		public function Time(sington:Sington)
		{
			//TODO: implement function
			super(S_Timer);
			if(Instance){
				throw new Error("this sington class pleass using GetInstance() get instance");
			}
			Instance=this;
			init();
		}
		private function init():void{
			list=new Vector.<S_Timer>;
			timer=new Timer(1);
			timer.addEventListener(TimerEvent.TIMER,E_Timeout);
			timer.start();
		}
		/**
		 * 检查 
		 * 
		 */
		private function E_Timeout(event:TimerEvent):void{
			var i:int=0,nLen:int=list.length;
			while(i<nLen){
				if(list[i].PoolStatus==Pool.USING){
					list[i].CheckTimeout();
				}
				i++;
			}
		} 
		
		
		
		public function D_setTimer(Complete:Function,timer:uint,count:uint=0xFFFFFFFF,...params):S_Timer{
			var result:S_Timer=S_Timer(Creater());
			var arr:Array=[Complete,timer,count];
			arr=arr.concat(params);
			return setTimer.apply(this,arr);
		}
		
		
		
		/**
		 * 执行定时器 
		 * @param timer
		 * @param Complete
		 * @param params
		 * @return 
		 * 
		 */
		public function D_setTimeout(Complete:Function,timer:uint,...params):S_Timer{
			var result:S_Timer=S_Timer(Creater());
			var arr:Array=[Complete,timer];
			arr=arr.concat(params);
			return setTimeout.apply(this,arr);
		}
		
		public function AddTable(timer:S_Timer):void{
			
			list.push(timer);
		}
		
		
		
		
		
		
		
		
		/**
		 * 设置定时器 
		 * @param timer
		 * @param count
		 * @param Complete
		 * @param params
		 * 
		 */
		static public function setTimer(Complete:Function,timer:uint,count:uint=0xFFFFFFFF,...params):S_Timer{
			var result:S_Timer=S_Timer(GetInstance().Creater());
			var arr:Array=[Complete,timer,count];
			arr=arr.concat(params);
			result.setTimer.apply(null,arr);
			GetInstance().AddTable(result);
			return result;
		}
		
		/**
		 * 执行定时器 
		 * @param timer
		 * @param Complete
		 * @param params
		 * @return 
		 * 
		 */
		static public function setTimeout(Complete:Function ,timer:uint, ...params):S_Timer{
			var result:S_Timer=S_Timer(GetInstance().Creater());
			var arr:Array=[Complete,timer];
			arr=arr.concat(params)
			result.setTimeout.apply(null,arr);
			GetInstance().AddTable(result);
			return result;
		}
		/**
		 * 获取单例 
		 * @return 
		 * 
		 */
		static public function GetInstance():Time{
			if(!Instance){
				new Time(new Sington());
			}
			return Instance;
		}
		
		
		
	}
}

class Sington{}